package oc.tm.sg.admin.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.smart4j.framework.tx.annotation.Service;

import oc.tm.sg.admin.govern.BootstrapGovern;
import oc.tm.sg.admin.govern.domain.SgService;
import oc.tm.sg.admin.govern.domain.support.ConsumerService;
import oc.tm.sg.admin.govern.domain.support.ProviderService;
import oc.tm.sg.admin.web.service.ServicesService;
import oc.tm.sg.core.util.StringUtils;
import oc.tm.sg.registry.common.Constants;
import oc.tm.sg.registry.common.URL;

/**
 * 服务控制器
 * 
 * @author lry
 */
@Service
public class ServicesServiceImpl implements ServicesService {
	BootstrapGovern registryContainer=BootstrapGovern.INSTANCE;
	/**
	 * 服务列表
	 * 
	 * @return
	 */
	public List<SgService> getServices() {
		List<SgService> rows = new ArrayList<SgService>();
		Set<String> services = registryContainer.getServices();

		if (services != null && services.size() > 0) {
			SgService dubboService;
			for (String service : services) {
				dubboService = new SgService();
				dubboService.setName(service);

				List<URL> providers = registryContainer.getProvidersByService(service);
				int providerSize = providers == null ? 0 : providers.size();
				dubboService.setProviderCount(providerSize);

				List<URL> consumers = registryContainer.getConsumersByService(service);
				int consumerSize = consumers == null ? 0 : consumers.size();
				dubboService.setConsumerCount(consumerSize);

				if (providerSize > 0) {
					URL provider = providers.iterator().next();
					dubboService.setApplication(provider.getParameter(Constants.APPLICATION_KEY, ""));
					dubboService.setOwner(provider.getParameter("owner", ""));
					dubboService.setOrganization((provider.hasParameter("organization") ? provider.getParameter("organization") : ""));
					
					dubboService.setGroup(provider.getParameter("group", ""));
					dubboService.setMethod(provider.getParameter("method", ""));
					dubboService.setMsg(provider.getParameter("msg", ""));
					dubboService.setTitle(provider.getParameter("title", ""));
				}

				rows.add(dubboService);
			}
		}

		return rows;
	}

	/**
	 * 服务提供者列表
	 * 
	 * @param service
	 * @return
	 */
	public List<ProviderService> providers(String service) {
		List<URL> providers = registryContainer.getProvidersByService(service);
		List<ProviderService> rows = new ArrayList<ProviderService>();
		if (providers != null && providers.size() > 0) {
			for (URL u : providers) {
				ProviderService pService = new ProviderService();
				pService.setAddress(u.getAddress());
				pService.setApplication(u.getParameter("application", ""));
				pService.setOrganization(u.getParameter("organization", ""));
				pService.setOwner(u.getParameter("owner", ""));

				pService.setGroup(u.getParameter("group", ""));
				pService.setInterfa(u.getParameter("interface", u.getPath()));
				pService.setVersion(u.getParameter("version", ""));

				pService.setEurl(u.toFullString());
				rows.add(pService);
			}
		}

		return rows;
	}

	/**
	 * 服务消费者列表
	 * 
	 * @param service
	 * @return
	 */
	public List<ConsumerService> consumers(String service) {
		List<URL> consumers = registryContainer.getConsumersByService(service);
		List<ConsumerService> rows = new ArrayList<ConsumerService>();
		if (consumers != null && consumers.size() > 0) {
			for (URL u : consumers) {
				ConsumerService cService = new ConsumerService();
				String pid = u.getParameter("pid", "");
				if (StringUtils.isNotBlank(pid)) {
					pid = "@" + pid;
				} else {
					pid = "";
				}
				cService.setAddress(u.getAddress() + pid);
				cService.setApplication(u.getParameter("application", ""));
				cService.setOrganization(u.getParameter("organization", ""));
				cService.setOwner(u.getParameter("owner", ""));

				cService.setGroup(u.getParameter("group", ""));
				cService.setInterfa(u.getParameter("interface", u.getPath()));
				cService.setVersion(u.getParameter("version", ""));

				cService.setEurl(u.toFullString());
				rows.add(cService);
			}
		}

		return rows;
	}
}
